package net.dl.ss.web.tags;

import java.io.IOException;

import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;

import net.dl.ss.web.ResourceBundleHelper;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * @author yakovlev.yuriy@gmail.com
 * @since 12.8.2008
 */
public class MessageTag extends TagSupport {
    private String key;

    public MessageTag() {
        super();
    }

    public void setKey(String key) {
        this.key = key;
    }

    public int doEndTag() throws JspTagException {
    	ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(pageContext.getServletContext());
    	ResourceBundleHelper helper = (ResourceBundleHelper) ctx.getBean("resourceBundleHelper");
    	String value = helper.getMessage(key);

    	try {
            pageContext.getOut().write(value);
        } catch (IOException ex) {
            throw new JspTagException(ex.getMessage());
        }
        return EVAL_PAGE;
    }

}
